﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.IO;

namespace DevExpress.MailClient.Win.Controls.File_tabs
{
    public partial class ucFileDynamicIndeciesDetails : PRO_DOCS.BLL.WIN.Templates.DevxPermissionUserControlTemplate
    {
        public bool SHOW_DOCUMENT_TYPE
        { set { tlpDocumentTypeSection.Visible = value; } }

        Dictionary<int, PRO_DOCS.DAL.Database.ARCHIVE_DOCUMENT_TYPE> ARCHIVE_DEPARTMENT_DOCUMENT_TYPES
        { get; set; }
        private PRO_DOCS.DAL.Database.ARCHIVE_DOCUMENT_TYPE _aRCHIVE_DOCUMENT_TYPE;
        public PRO_DOCS.DAL.Database.ARCHIVE_DOCUMENT_TYPE ARCHIVE_DOCUMENT_TYPE
        {
            get { return _aRCHIVE_DOCUMENT_TYPE; }
            set
            {
                if (value != null)
                {

                    if (!value.ARCHIVE_DOCUMENT_TYPE_ID.Equals(0))
                    {
                        if (!value.ARCHIVE_DOCUMENT_TYPE_ID.Equals(_aRCHIVE_DOCUMENT_TYPE.ARCHIVE_DOCUMENT_TYPE_ID))
                        {
                            _aRCHIVE_DOCUMENT_TYPE = value;

                            comboDocumentTypes.EditValue = _aRCHIVE_DOCUMENT_TYPE.ARCHIVE_DOCUMENT_TYPE_NAME;
                        }
                    }
                    else
                    {
                        _aRCHIVE_DOCUMENT_TYPE = new PRO_DOCS.DAL.Database.ARCHIVE_DOCUMENT_TYPE();
                    }

                }
            }
        }

        List<string> CURRENT_ARCHIVES_COPYING_FOLDERS
        {
            get;
            set;
        }

        private PRO_DOCS.DAL.Database.ARCHIVE_DEPARTMENT_FILE _cURRENT_ARCHIVE_DEPARTMENT_FILE;
        [Browsable(false)]
        public PRO_DOCS.DAL.Database.ARCHIVE_DEPARTMENT_FILE CURRENT_ARCHIVE_DEPARTMENT_FILE
        {
            get { return _cURRENT_ARCHIVE_DEPARTMENT_FILE; }
            set
            {
                _cURRENT_ARCHIVE_DEPARTMENT_FILE = value;
                if (_cURRENT_ARCHIVE_DEPARTMENT_FILE != null)
                {
                    if (!_cURRENT_ARCHIVE_DEPARTMENT_FILE.ARCHIVE_DEPARTMENT_FILE_ID.Equals(Guid.Empty))
                    {
                        CURRENT_ARCHIVE_DEPARTMENT = _cURRENT_ARCHIVE_DEPARTMENT_FILE.ARCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE.ARCHIVE_DEPARTMENT;
                        ARCHIVE_DOCUMENT_TYPE = _cURRENT_ARCHIVE_DEPARTMENT_FILE.ARCHIVE_DOCUMENT_TYPE;
                    }
                }
            }
        }


        private PRO_DOCS.DAL.Database.ARCHIVE_DEPARTMENT_FILE_HISTORY _cURRENT_ARCHIVE_DEPARTMENT_FILE_HISTORY;
        public PRO_DOCS.DAL.Database.ARCHIVE_DEPARTMENT_FILE_HISTORY CURRENT_ARCHIVE_DEPARTMENT_FILE_HISTORY
        {
            get { return _cURRENT_ARCHIVE_DEPARTMENT_FILE_HISTORY; }
            set
            {
                _cURRENT_ARCHIVE_DEPARTMENT_FILE_HISTORY = value;
                if (_cURRENT_ARCHIVE_DEPARTMENT_FILE_HISTORY != null)
                {
                    SET_FILE_INDECIES();
                }
            }
        }

        private bool CURRENT_TABLE_IS_EMPTY
        {
            get;
            set;
        }

        private List<PRO_DOCS.DAL.Database.ARCHIVE_DEPARTMENT_FILE_HISTORY_INDEXES_VALUE> aRCHIVE_DEPARTMENT_FILE_HISTORY_INDEXES_VALUES;
        private List<PRO_DOCS.DAL.Database.ARCHIVE_DEPARTMENT_FILE_HISTORY_INDEXES_VALUE> ARCHIVE_DEPARTMENT_FILE_HISTORY_INDEXES_VALUES
        {
            get { return aRCHIVE_DEPARTMENT_FILE_HISTORY_INDEXES_VALUES; }
            set
            {
                aRCHIVE_DEPARTMENT_FILE_HISTORY_INDEXES_VALUES = value;

                if (aRCHIVE_DEPARTMENT_FILE_HISTORY_INDEXES_VALUES.Count > 0)
                {
                    Dictionary<Guid, string> gET_DYNAMIC_INDECIES_INPUT_VALUES = aRCHIVE_DEPARTMENT_FILE_HISTORY_INDEXES_VALUES.ToDictionary(k => k.ARCHIVE_DEPARTMENT_DOCUMENT_TYPE_INDEX_ID, v => v.ARCHIVE_DEPARTMENT_FILE_HISTORY_INDEX_VALUE);
                    new PRO_DOCS.BLL.WIN.CustomDataTypes().SET_DYNAMIC_INDECIES_INPUT_VALUES(tblDynamicInfos, gET_DYNAMIC_INDECIES_INPUT_VALUES);
                    CURRENT_TABLE_IS_EMPTY = false;
                }
                else
                {
                    CLEAR_TABLE_CONTROLS();
                }
            }
        }

        private void CLEAR_TABLE_CONTROLS()
        {
            if (!CURRENT_TABLE_IS_EMPTY)
            {
                new PRO_DOCS.BLL.WIN.CustomDataTypes().GET_DOCUMENT_TYPE_STRUCTURE(tblDynamicInfos, ARCHIVE_DOCUMENT_TYPE);
                CURRENT_TABLE_IS_EMPTY = true;
            }
        }

        public void SET_FILE_INDECIES()
        {
            ARCHIVE_DEPARTMENT_FILE_HISTORY_INDEXES_VALUES = new List<PRO_DOCS.DAL.Database.ARCHIVE_DEPARTMENT_FILE_HISTORY_INDEXES_VALUE>();

            ARCHIVE_DEPARTMENT_FILE_HISTORY_INDEXES_VALUES = (from indecies in PRO_DOCSDatabaseDataContext.ARCHIVE_DEPARTMENT_FILE_HISTORY_INDEXES_VALUEs
                                                              where indecies.ARCHIVE_DEPARTMENT_FILE_HISTORY_ID.Equals(CURRENT_ARCHIVE_DEPARTMENT_FILE_HISTORY.ARCHIVE_DEPARTMENT_FILE_HISTORY_ID)
                                                              select indecies).ToList();//.ARCHIVE_DEPARTMENT_FILE_HISTORY_INDEXES_VALUEs.ToList();
        }

        //private void SAVE_FILE_INDECIES()
        //{ }

        public ucFileDynamicIndeciesDetails()
        {
            InitializeComponent();
            ARCHIVE_DOCUMENT_TYPE = new PRO_DOCS.DAL.Database.ARCHIVE_DOCUMENT_TYPE();
            CURRENT_ARCHIVE_DEPARTMENT_FILE = new PRO_DOCS.DAL.Database.ARCHIVE_DEPARTMENT_FILE();
            CURRENT_ARCHIVE_DEPARTMENT = new PRO_DOCS.DAL.Database.ARCHIVE_DEPARTMENT();
            CURRENT_ARCHIVES_COPYING_FOLDERS = new List<string>();

        }

        internal PRO_DOCS.DAL.Database.ARCHIVE_DEPARTMENT_FILE_HISTORY SAVE_SELECTED_FILE_DYNAMIC_INDICIES()
        {
            bool fileIndeciesValid = new PRO_DOCS.BLL.WIN.CustomDataTypes().VALIDATE_DYNAMIC_INDECIES_INPUT_VALUES(tblDynamicInfos, dxErrorProvider1);


            PRO_DOCS.DAL.Database.ARCHIVE_DEPARTMENT_FILE_HISTORY cURRENT_ARCHIVE_DEPARTMENT_FILE_HISTORY = null;

            if (fileIndeciesValid)
            {


                Guid aRCHIVE_DEPARTMENT_FILE_HISTORY_ID = Guid.NewGuid();
                bool fileSaved = false;
                if (CURRENT_ARCHIVE_DEPARTMENT_FILE != null)
                {
                    if (!CURRENT_ARCHIVE_DEPARTMENT_FILE.ARCHIVE_DEPARTMENT_FILE_ID.Equals(Guid.Empty))
                    {
                        Dictionary<Guid, string> gET_DYNAMIC_INDECIES_INPUT_VALUES = new PRO_DOCS.BLL.WIN.CustomDataTypes().GET_DYNAMIC_INDECIES_INPUT_VALUES(tblDynamicInfos);

                        #region SAVE IF THE FILE IS 1st TIME SAVING
                        if (CURRENT_ARCHIVE_DEPARTMENT_FILE_HISTORY.ARCHIVE_DEPARTMENT_FILE_HISTORY_INDEXES_VALUEs.Count.Equals(0))
                        {
                            aRCHIVE_DEPARTMENT_FILE_HISTORY_ID = CURRENT_ARCHIVE_DEPARTMENT_FILE_HISTORY.ARCHIVE_DEPARTMENT_FILE_HISTORY_ID;

                            SAVE_FILE_HISTORY_DYNAMIC_INDECIES(aRCHIVE_DEPARTMENT_FILE_HISTORY_ID, gET_DYNAMIC_INDECIES_INPUT_VALUES);

                            fileSaved = true;
                        }
                        #endregion

                        #region SAVE IF IT WAS PREVIOUSLY SAVED
                        else
                        {
                            PRO_DOCS.DocumentManagementSystem.CLIENT.WIN.Forms.frmFileHistoryDetails frmFileHistoryDetails = new PRO_DOCS.DocumentManagementSystem.CLIENT.WIN.Forms.frmFileHistoryDetails(CURRENT_ARCHIVE_DEPARTMENT_FILE);

                            if (frmFileHistoryDetails.ShowDialog().Equals(DialogResult.OK))
                            {
                                //1. create a new file history
                                PRO_DOCS.DAL.Database.ARCHIVE_DEPARTMENT_FILE_HISTORY aRCHIVE_DEPARTMENT_FILE_HISTORY = new PRO_DOCS.DAL.Database.ARCHIVE_DEPARTMENT_FILE_HISTORY();
                                aRCHIVE_DEPARTMENT_FILE_HISTORY.ARCHIVE_DEPARTMENT_FILE_ID = CURRENT_ARCHIVE_DEPARTMENT_FILE_HISTORY.ARCHIVE_DEPARTMENT_FILE_ID.Value;
                                aRCHIVE_DEPARTMENT_FILE_HISTORY.ARCHIVE_DEPARTMENT_FILE_HISTORY_ID = aRCHIVE_DEPARTMENT_FILE_HISTORY_ID;
                                aRCHIVE_DEPARTMENT_FILE_HISTORY.ARCHIVE_DEPARTMENT_FILE_HISTORY_CHANGING_TYPE_ID = 3;
                                aRCHIVE_DEPARTMENT_FILE_HISTORY.ARCHIVE_DEPARTMENT_FILE_ORIGINAL_NAME = CURRENT_ARCHIVE_DEPARTMENT_FILE_HISTORY.ARCHIVE_DEPARTMENT_FILE_ORIGINAL_NAME;
                                aRCHIVE_DEPARTMENT_FILE_HISTORY.ARCHIVE_DEPARTMENT_FILE_EXTENSION = CURRENT_ARCHIVE_DEPARTMENT_FILE_HISTORY.ARCHIVE_DEPARTMENT_FILE_EXTENSION;
                                aRCHIVE_DEPARTMENT_FILE_HISTORY.ARCHIVE_DEPARTMENT_FILE_HISTORY_UPLOADED_BY_USER_ID = PRO_DOCS.BLL.WIN.Authentication.LOGGED_IN_EMPLOYEE_DETAILS.LOGIN_USER.LOGIN_USER_ID;
                                aRCHIVE_DEPARTMENT_FILE_HISTORY.ARCHIVE_DEPARTMENT_FILE_HISTORY_FILE_SIZE = CURRENT_ARCHIVE_DEPARTMENT_FILE_HISTORY.ARCHIVE_DEPARTMENT_FILE_HISTORY_FILE_SIZE;
                                aRCHIVE_DEPARTMENT_FILE_HISTORY.ARCHIVE_DEPARTMENT_FILE_HISTORY_UPLOADED_DATE = DateTime.Now;
                                aRCHIVE_DEPARTMENT_FILE_HISTORY.ARCHIVE_DEPARTMENT_FILE_HISTORY_NOTES = frmFileHistoryDetails.HISTORY_NOTES;

                                aRCHIVE_DEPARTMENT_FILE_HISTORY.IS_DELETED = false;

                                //3. save the Physical file with the new name
                                if (SAVE_PHYSICAL_FILE_NEW_HISTORY(aRCHIVE_DEPARTMENT_FILE_HISTORY.ARCHIVE_DEPARTMENT_FILE_HISTORY_ID))
                                {

                                    PRO_DOCSDatabaseDataContext.proc_ARCHIVE_DEPARTMENT_FILE_HISTORYInsert
                                        (
                                        aRCHIVE_DEPARTMENT_FILE_HISTORY.ARCHIVE_DEPARTMENT_FILE_HISTORY_ID,
                                        aRCHIVE_DEPARTMENT_FILE_HISTORY.ARCHIVE_DEPARTMENT_FILE_ID.Value,
                                        aRCHIVE_DEPARTMENT_FILE_HISTORY.ARCHIVE_DEPARTMENT_FILE_HISTORY_CHANGING_TYPE_ID.Value,
                                        aRCHIVE_DEPARTMENT_FILE_HISTORY.ARCHIVE_DEPARTMENT_FILE_ORIGINAL_NAME,
                                        aRCHIVE_DEPARTMENT_FILE_HISTORY.ARCHIVE_DEPARTMENT_FILE_EXTENSION,
                                        aRCHIVE_DEPARTMENT_FILE_HISTORY.ARCHIVE_DEPARTMENT_FILE_HISTORY_UPLOADED_BY_USER_ID.Value,
                                        aRCHIVE_DEPARTMENT_FILE_HISTORY.ARCHIVE_DEPARTMENT_FILE_HISTORY_UPLOADED_DATE.Value,
                                        aRCHIVE_DEPARTMENT_FILE_HISTORY.ARCHIVE_DEPARTMENT_FILE_HISTORY_FILE_SIZE.Value,
                                        aRCHIVE_DEPARTMENT_FILE_HISTORY.ARCHIVE_DEPARTMENT_FILE_HISTORY_NOTES,
                                        aRCHIVE_DEPARTMENT_FILE_HISTORY.IS_DELETED.Value);

                                    //2. save the file history indecies
                                    SAVE_FILE_HISTORY_DYNAMIC_INDECIES(aRCHIVE_DEPARTMENT_FILE_HISTORY_ID, gET_DYNAMIC_INDECIES_INPUT_VALUES);

                                    fileSaved = true;
                                }
                                else
                                {
                                    XtraMessageBox.Show(string.Format("The storage media for the \"{0}\" department was not found.{1}Please make sure that you are connected to the network or contact your network administrator", CURRENT_ARCHIVE_DEPARTMENT_FILE.ARCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE.ARCHIVE_DEPARTMENT.ARCHIVE_DEPARTMENT_NAME, Environment.NewLine),
                                "Storage Media was not found",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Warning);
                                }
                            }
                        }

                        #endregion

                    }
                }
                if (fileSaved)
                {

                    CURRENT_ARCHIVE_DEPARTMENT_FILE_HISTORY = (from history in PRO_DOCSDatabaseDataContext.ARCHIVE_DEPARTMENT_FILE_HISTORies
                                                               where history.ARCHIVE_DEPARTMENT_FILE_HISTORY_ID.Equals(aRCHIVE_DEPARTMENT_FILE_HISTORY_ID)
                                                               select history).FirstOrDefault();
                    cURRENT_ARCHIVE_DEPARTMENT_FILE_HISTORY = CURRENT_ARCHIVE_DEPARTMENT_FILE_HISTORY;
                    SET_FILE_INDECIES();

                }
                else
                {

                }

            }
            else
            {
                XtraMessageBox.Show("Saving cannot be completed because some required field left blank,\r\nPlease set values for these indecies before continue", "Saving Truncated", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            return cURRENT_ARCHIVE_DEPARTMENT_FILE_HISTORY;
        }

        private bool SAVE_PHYSICAL_FILE_NEW_HISTORY(Guid guid)
        {
            bool fileUploaded = false;
            //the new fileId to be named with
            string newCreatedPhysicalFileName = guid.ToString();
            //the original file path

            bool fileFound = false;
            FileInfo currentConsumedFile = null;
            List<PRO_DOCS.DAL.Database.ARCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY_PHYSICAL_PATHE> aRCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY_PHYSICAL_PATHES = CURRENT_ARCHIVE_DEPARTMENT_FILE.ARCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE.ARCHIVE_DEPARTMENT.ARCHIVE_DEPARTMENTS_STORAGES_HEIRARCHy.ARCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY_PHYSICAL_PATHEs.ToList();

            for (int i = 0; i < aRCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY_PHYSICAL_PATHES.Count; i++)
            {
                if (new DirectoryInfo(aRCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY_PHYSICAL_PATHES[i].ARCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY_PHYSICAL_PATH).Exists)
                {
                    CONTINUE_UPLOADING = true;
                    CURRENT_ARCHIVES_COPYING_FOLDERS.Add(aRCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY_PHYSICAL_PATHES[i].ARCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY_PHYSICAL_PATH);
                    if (!fileFound)
                    {
                        currentConsumedFile = new FileInfo(Path.Combine(aRCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY_PHYSICAL_PATHES[i].ARCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY_PHYSICAL_PATH, CURRENT_ARCHIVE_DEPARTMENT_FILE_HISTORY.ARCHIVE_DEPARTMENT_FILE_HISTORY_ID.ToString()));//.FileInfto;
                        fileFound = true;
                    }
                    if (fileFound)
                    {
                        if (!fileUploaded)
                        {
                            try
                            {
                                currentConsumedFile.CopyTo(Path.Combine(aRCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY_PHYSICAL_PATHES[i].ARCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY_PHYSICAL_PATH, newCreatedPhysicalFileName));
                                fileUploaded = true;
                                break;
                            }
                            catch (Exception ex)
                            {

                            }
                        }
                    }

                }
            }


            return fileUploaded;
        }

        private void SAVE_FILE_HISTORY_DYNAMIC_INDECIES(Guid aRCHIVE_DEPARTMENT_FILE_HISTORY_ID, Dictionary<Guid, string> gET_DYNAMIC_INDECIES_INPUT_VALUES)
        {
            //1. save the file baseic data

            //2. save the file dynamic data
            foreach (KeyValuePair<Guid, string> dYNAMIC_INDECIES_INPUT_VALUE in gET_DYNAMIC_INDECIES_INPUT_VALUES)
            {
                PRO_DOCS.DAL.Database.ARCHIVE_DEPARTMENT_FILE_HISTORY_INDEXES_VALUE aRCHIVE_DEPARTMENT_FILE_HISTORY_INDEXES_VALUE = new PRO_DOCS.DAL.Database.ARCHIVE_DEPARTMENT_FILE_HISTORY_INDEXES_VALUE();

                aRCHIVE_DEPARTMENT_FILE_HISTORY_INDEXES_VALUE.ARCHIVE_DEPARTMENT_FILE_HISTORY_INDEX_VALUE_ID = Guid.NewGuid();
                aRCHIVE_DEPARTMENT_FILE_HISTORY_INDEXES_VALUE.ARCHIVE_DEPARTMENT_DOCUMENT_TYPE_INDEX_ID = dYNAMIC_INDECIES_INPUT_VALUE.Key;
                aRCHIVE_DEPARTMENT_FILE_HISTORY_INDEXES_VALUE.ARCHIVE_DEPARTMENT_FILE_HISTORY_ID = aRCHIVE_DEPARTMENT_FILE_HISTORY_ID;
                aRCHIVE_DEPARTMENT_FILE_HISTORY_INDEXES_VALUE.ARCHIVE_DEPARTMENT_FILE_HISTORY_INDEX_VALUE = dYNAMIC_INDECIES_INPUT_VALUE.Value;

                PRO_DOCSDatabaseDataContext.proc_ARCHIVE_DEPARTMENT_FILE_HISTORY_INDEXES_VALUESInsert
                    (
                        aRCHIVE_DEPARTMENT_FILE_HISTORY_INDEXES_VALUE.ARCHIVE_DEPARTMENT_FILE_HISTORY_INDEX_VALUE_ID,
                        aRCHIVE_DEPARTMENT_FILE_HISTORY_INDEXES_VALUE.ARCHIVE_DEPARTMENT_DOCUMENT_TYPE_INDEX_ID,
                        aRCHIVE_DEPARTMENT_FILE_HISTORY_INDEXES_VALUE.ARCHIVE_DEPARTMENT_FILE_HISTORY_ID,
                        aRCHIVE_DEPARTMENT_FILE_HISTORY_INDEXES_VALUE.ARCHIVE_DEPARTMENT_FILE_HISTORY_INDEX_VALUE
                    );

            }
        }

        public bool CONTINUE_UPLOADING { get; set; }

        private void ucFileDynamicIndeciesDetails_Load(object sender, EventArgs e)
        {
            #region DOCUMENT TYPES FOR THE CURRENT DEPARTMENT


            //RELOAD_ARCHIVE_DOCUMENT_TYPES();
            #endregion
        }

        private void RELOAD_ARCHIVE_DOCUMENT_TYPES()
        {

            ARCHIVE_DEPARTMENT_DOCUMENT_TYPES = new Dictionary<int, PRO_DOCS.DAL.Database.ARCHIVE_DOCUMENT_TYPE>();
            List<PRO_DOCS.DAL.Database.proc_ARCHIVE_DEPARTMENT_DOCUMENT_TYPESLoadAllByARCHIVE_DEPARTMENT_IDResult> aRCHIVE_DOCUMENT_TYPES = PRO_DOCSDatabaseDataContext.proc_ARCHIVE_DEPARTMENT_DOCUMENT_TYPESLoadAllByARCHIVE_DEPARTMENT_ID(CURRENT_ARCHIVE_DEPARTMENT.ARCHIVE_DEPARTMENT_ID).ToList();

            comboDocumentTypes.Properties.Items.BeginUpdate();
            comboDocumentTypes.Properties.Items.Clear();

            int nmbr = 0;
            for (int i = 0; i < aRCHIVE_DOCUMENT_TYPES.Count; i++)
            {
                if (!aRCHIVE_DOCUMENT_TYPES[i].IS_DELETED.Value)
                {
                    PRO_DOCS.DAL.Database.ARCHIVE_DOCUMENT_TYPE aRCHIVE_DOCUMENT_TYPE = (from doctype in PRO_DOCSDatabaseDataContext.ARCHIVE_DOCUMENT_TYPEs
                                                                                         where doctype.ARCHIVE_DOCUMENT_TYPE_ID.Equals(aRCHIVE_DOCUMENT_TYPES[i].ARCHIVE_DEPARTMENT_DOCUMNET_TYPE_ID)
                                                                                         select doctype).FirstOrDefault();// .proc_ARCHIVE_DOCUMENT_TYPESLoadByPrimaryKey(aRCHIVE_DOCUMENT_TYPES[i].ARCHIVE_DEPARTMENT_DOCUMNET_TYPE_ID).FirstOrDefault();

                    ARCHIVE_DEPARTMENT_DOCUMENT_TYPES.Add(nmbr, aRCHIVE_DOCUMENT_TYPE);
                    comboDocumentTypes.Properties.Items.Add(aRCHIVE_DOCUMENT_TYPE.ARCHIVE_DOCUMENT_TYPE_NAME);
                    nmbr++;
                }
            }

            comboDocumentTypes.Properties.Items.EndUpdate();
        }

        private void comboDocumentTypes_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboDocumentTypes.SelectedIndex > -1)
            {
                ARCHIVE_DOCUMENT_TYPE = ARCHIVE_DEPARTMENT_DOCUMENT_TYPES[comboDocumentTypes.SelectedIndex];
                new PRO_DOCS.BLL.WIN.CustomDataTypes().GET_DOCUMENT_TYPE_STRUCTURE(tblDynamicInfos, ARCHIVE_DOCUMENT_TYPE);
            }
        }

        public PRO_DOCS.DAL.Database.ARCHIVE_DEPARTMENT cURRENT_ARCHIVE_DEPARTMENT;
        public PRO_DOCS.DAL.Database.ARCHIVE_DEPARTMENT CURRENT_ARCHIVE_DEPARTMENT
        {
            get { return cURRENT_ARCHIVE_DEPARTMENT; }
            set
            {
                if (value != null)
                {
                    if (value.ARCHIVE_DEPARTMENT_ID > 0)
                    {
                        //if (cURRENT_ARCHIVE_DEPARTMENT != null)
                        {
                            //if (cURRENT_ARCHIVE_DEPARTMENT.ARCHIVE_DEPARTMENT_ID != value.ARCHIVE_DEPARTMENT_ID)
                            {
                                cURRENT_ARCHIVE_DEPARTMENT = value;

                                RELOAD_ARCHIVE_DOCUMENT_TYPES();
                            }
                        }
                        //else
                        //{

                        //    cURRENT_ARCHIVE_DEPARTMENT = value;

                        //    RELOAD_ARCHIVE_DOCUMENT_TYPES();
                        //}
                    }
                }
            }
        }
    }
}
